package com.quectel.cms.controller.workorder;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.workorder.dto.WorkOrderLogDto;
import com.quectel.core.module.workorder.service.WorkOrderLogService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-19 13:06:45
 */
@RestController
@RequestMapping("workOrderLog")
@Tag(name = "WorkOrderLogController", description = "工单操作流水日志相关api")
public class WorkOrderLogController extends BaseController {

    @DubboReference
    private WorkOrderLogService workOrderLogService;

    /**
     * 根据参数查询工单操作流水日志分页列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "workOrderLog:list")
    @Operation(summary = "根据参数查询工单操作流水日志分页列表")
    public Response<Page<WorkOrderLogDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = workOrderLogService.queryTotal(request.getParams());

        List<WorkOrderLogDto> list = null;
        if (total > 0) {
            list = workOrderLogService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<WorkOrderLogDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<WorkOrderLogDto>>ok().wrap(page);
    }


    /**
     * 根据id查询工单操作流水日志
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "workOrderLog:info")
    @Operation(summary = "根据id查询工单操作流水日志")
    public Response<WorkOrderLogDto> info(@PathVariable("id") Long id) {

        if (workOrderLogService
                .queryTotal(super.fillAuthorityData(new HashMap<String, Object>() {{
                    put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
                }})) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        WorkOrderLogDto workOrderLogDto = workOrderLogService.selectById(id);

        return Response.<WorkOrderLogDto>ok().wrap(workOrderLogDto);
    }

    /**
     * 保存工单操作流水日志
     */
    @PostMapping("save")
    @RequirePermissions(values = "workOrderLog:edit")
    @Operation(summary = "保存工单操作流水日志")
    public Response<Object> save(@RequestBody @Validated WorkOrderLogDto workOrderLogDto) {

        //填充基础数据
        fillSaveCommonData(workOrderLogDto);

        workOrderLogService.save(workOrderLogDto);

        return Response.ok();
    }

    /**
     * 修改工单操作流水日志
     */
    @PostMapping("update")
    @RequirePermissions(values = "workOrderLog:edit")
    @Operation(summary = "修改工单操作流水日志")
    public Response<Object> update(@RequestBody @Validated WorkOrderLogDto workOrderLogDto) {

        if (workOrderLogService
                .queryTotal(super.fillAuthorityData(new HashMap<String, Object>() {{
                    put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(workOrderLogDto.getId()));
                }})) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(workOrderLogDto);

        workOrderLogService.updateById(workOrderLogDto);

        return Response.ok();
    }

    /**
     * 批量删除工单操作流水日志
     */
    @PostMapping("delete")
    @RequirePermissions(values = "workOrderLog:delete")
    @Operation(summary = "批量删除工单操作流水日志")
    public Response<Object> delete(@RequestBody Long[] ids) {

        for (Long id : ids) {
            if (workOrderLogService
                    .queryTotal(super.fillAuthorityData(new HashMap<String, Object>() {{
                        put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
                    }})) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        workOrderLogService.deleteBatch(ids);

        return Response.ok();
    }
}
